export default {
  registerBridge () {
    // 注册事件监听
    function connectWebViewJavascriptBridge (callback) {
      if (window.WebViewJavascriptBridge) {
        callback(window.WebViewJavascriptBridge)
      } else {
        document.addEventListener('WebViewJavascriptBridgeReady', function () {
          callback(window.WebViewJavascriptBridge)
        }, false)
        if (window.WVJBCallbacks) {
          return window.WVJBCallbacks.push(callback)
        }
        window.WVJBCallbacks = [callback]
        var WVJBIframe = document.createElement('iframe')
        WVJBIframe.style.display = 'none'
        WVJBIframe.src = 'https://__bridge_loaded__'
        document.documentElement.appendChild(WVJBIframe)
        setTimeout(function () {
          document.documentElement.removeChild(WVJBIframe)
        }, 0)
      }
    }
    // 注册回调函数，第一次连接时调用 初始化函数
    connectWebViewJavascriptBridge(function (bridge) {
      bridge.init(function (message, responseCallback) {
        console.log('JS got a message', message)
        var data = {
          'Javascript Responds': 'CMYH!'
        }
        console.log('JS responding with', data)
        responseCallback(data)
      })
    })
  },
  showWebView (isshow) {
    // 显示webview
    if (window.WebViewJavascriptBridge) {
      window.WebViewJavascriptBridge.callHandler('showWebView', {'isShow': isshow}, function (response) {})
    } else {
      setTimeout(() => {
        this.showWebView(isshow)
      }, 50)
    }
  },
  setWebViewSize (w = 0, h = 0) {
    // 设置webview大小
    if (window.WebViewJavascriptBridge) {
      window.WebViewJavascriptBridge.callHandler('setWebViewSize', {'webViewWidth': w, 'webViewHeight': h}, function (response) {
        console.log(response)
      })
    } else {
      setTimeout(() => {
        this.setWebViewSize(w, h)
      }, 50)
    }
  },
  setWebViewPosition (x = 0, y = 0) {
    // 设置webview位置
    if (window.WebViewJavascriptBridge) {
      window.WebViewJavascriptBridge.callHandler('setWebViewPosition', {'left': x, 'top': y}, function (response) {
        console.log(response)
      })
    } else {
      setTimeout(() => {
        this.setWebViewPosition(x, y)
      }, 50)
    }
  },
  createNewWebView () {
    // 打开新webview
    if (window.WebViewJavascriptBridge) {
      window.WebViewJavascriptBridge.callHandler('createNewWebView', {'linkHref': this.$data.actdata.detailUrl}, function (response) {})
    } else {
      setTimeout(() => {
        this.createNewWebView()
      }, 50)
    }
  },
  getAddress () {
    // 获取手机通讯录
    if (window.WebViewJavascriptBridge) {
      window.WebViewJavascriptBridge.callHandler('getAddress', {}, function (responseData) {
        document.write(responseData)
      })
    } else {
      setTimeout(() => {
        this.getAddress()
      }, 50)
    }
  },
  deleteWebView () {
    // 关闭webview
    if (window.WebViewJavascriptBridge) {
      window.WebViewJavascriptBridge.callHandler('deleteWebView', {}, function (responseData) {})
    } else {
      setTimeout(() => {
        this.deleteWebView()
      }, 50)
    }
  },
  getUserMsg (succcall) {
    // 获取用户信息tokenString
    if (window.WebViewJavascriptBridge) {
      window.WebViewJavascriptBridge.callHandler('getUserMsg', {}, function (responseData) {
        succcall(responseData)
      })
    } else {
      setTimeout(() => {
        this.getUserMsg(succcall)
      }, 50)
    }
    // return new Promise((resolve, reject) => {
    //   if (window.WebViewJavascriptBridge) {
    //     window.WebViewJavascriptBridge.callHandler('getUserMsg', {}, function (responseData) {
    //       resolve(responseData)
    //     })
    //   } else {
    //     setTimeout(() => {
    //       this.getUserMsg()
    //     }, 50)
    //   }
    // })
  },
  wxshare (shareType = 0, imgData = '') {
    // 微信分享
    if (window.WebViewJavascriptBridge) {
      return new Promise((resolve, reject) => {
        window.WebViewJavascriptBridge.registerHandler('wxshareCallbak', function (data, responseCallback) {
          // IOS必需去注册（registerHandler）一个方法才能用
          resolve(JSON.parse(data).state)
        })
        window.WebViewJavascriptBridge.callHandler('wxshare', {'shareType': shareType, 'imgData': imgData}, function (responseData) {
          // Android直接主动调用该方法，可以主动传值
          resolve(JSON.parse(responseData).state)
        })
      })
    } else {
      setTimeout(() => {
        this.wxshare(shareType, imgData)
      }, 50)
    }
  }
}
